import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

public class UpLoadFile {

	public static void main(String[] args) {
		   Map<String, Object> info = new HashMap<String, Object>();
		   info.put("a", "apple");
		   info.put("b", "banana");
		   Map<String, Object> meta = new HashMap<String, Object>();
		   meta.put("width", 100);
		   meta.put("height", 200);
		   //参数设置
		   Dentry request = new Dentry();
		   ExtendUploadData requestData = new ExtendUploadData();
		   //request.setDentryId("96340084-775d-4740-ae2f-b2dea289618b");       //如果有传此项，将会覆盖旧文件数据 dentryId和filePath二选一 覆盖上传时不用传parentId，path，name
		   //requestData.setFilePath("/sample/1.jpg");				//如果有传此项，将会覆盖旧文件数据，dentryId和filePath二选一 覆盖上传时不用传parentId，path，name
		   //request.setParentId("96340084-775d-4740-ae2f-b2dea289618b");	//父目录项id（UUID），（path 和 parent_id 二选一）
		   requestData.setExpireDays(0);					//过期天数，0-永不过期，可选，默认：0
		   request.setPath("/sample");						//父目录项路径，支持自动创建目录，（path 和 parent_id 二选一）
		   request.setName("name");						//文件名，包括后缀名 必选
		   request.setOtherName("otherName");					//备注名  可选
		   request.setInfo(info);						//自定义元数据，如：{tags: [xx,xx], title: xx, note: xx, content: xx},可选   
		   request.getInode().setMd5("96340084-775d-4740-ae2f-b2dea289618b");   //整个文件的MD5码，文件秒传时必选
		   request.getInode().setMeta(meta);					//图片，音频，视频等元数据： {width: 1024, height: 768} ，可选
		   request.getInode().setMime("image/jpeg");				//文件mime                可选
		   request.setScope(0);							//0-私密，1-公开，默认：0，可选
		   
		   //调用
		   Dentry result = request.upload(Config.serviceName,"F:/res/j1.jpg", requestData, Config.session,new UploadProgressCallBack() {				
			@Override
			public void onUploadProgressCallBack(Long bytesWritten,
					Long totalBytesWritten, Long totalBytesExpectedToWrite) {		
				BigDecimal b = new BigDecimal(Double.valueOf(totalBytesExpectedToWrite) / Double.valueOf(totalBytesWritten) * 100D);
			double process =b.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
			System.out.println("上传了"+process+"%");
			}
		  });

		  //通过token方式上传
		  long uid = 10000;		//上传都的userId
		  String role = "user";		//上传的CS角色
		  int scope = 1;		///0-私密，1-公开
		  Policy policy = Policy.getUploadPolicy(uid,role, scope);
		  Dentry result = request.upload(Config.serviceName,"F:/res/j1.jpg", requestData,null,null,policy);
	}
}
